<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>最长公共子序列</title>
</head>
<body>
  <script>
    function fn(text1, text2) {
      let len1 = text1.length, len2 = text2.length
      let dp = new Array(len1 + 1).fill(0).map(() => new Array(len2 + 1).fill(0))
      for (let i = 1; i <= len1; i++)
        for (let j = 1; j <= len2; j++) {
          if (text1[i - 1] === text2[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1
          else dp[i][j] = Math.max(dp[i - 1][j], dp[i][j - 1])
        }
      return dp[len1][len2]
    }
    console.log(fn('abcde', 'ace'))
  </script>
</body>
</html>